import { createStore } from 'vuex';
export default createStore({
	state: { //存放状态
		url: 'https://mhs.qfxwl.com/api',
		url2: 'https://mhs.qfxwl.com/static/api',
		imgpath: 'https://mhs.qfxwl.com/static/api',
		storage: 'https://mhs.qfxwl.com/storage/',
		api: {
			globalData: '/Auth/getglobalData',
			index: '/Index/index.html',
			search: '/Index/search.html',
			detail: '/Detail/detail.html',
			auth: '/Auth/auth',
			onlogin: '/Auth/onlogin.html',
			authApp: '/Auth/authApp.html',
			logs: '/Logs/logs.html',
			address: '/My/address.html',
			do_address: '/My/do_address.html',
			get_address: '/My/get_address.html',
			up_tdefault: '/My/up_tdefault',
			addressdel: '/My/addressdel',
			uploadImages: '/My/uploadImages',
			do_data: '/My/do_data.html',
			mycoupon: '/Coupon/mycoupon.html',
			get_phb: '/Detail/get_phb.html',
			get_record: '/Detail/get_record.html',
			pay: '/Detail/pay.html',
			sendAddress: '/userbox/sendAddress.html',
			currency: '/My/currency.html',
			getprizelist: '/Detail/getprizelist.html',
			sendSmsCode: '/Coupon/sendSmsCode.html',
			checkSmsCode: '/User/mobilelogin.html',
			userReward: '/userbox/userReward.html',
			box: '/userbox/box.html'
		}
	},
	mutations: {
		tip(state, title, icon = 'none') {
			uni.showToast({
				title,
				icon,
				mask: true
			})
		},
		jump(state, url) {
			if (url == '/pages/user/user' || url == '/pages/index/index' || url == '/pages/plan/plan' || url ==
				'/pages/uploads/uploads') {
				uni.switchTab({
					url
				})
			} else {
				uni.navigateTo({
					url
				})
			}
		}
	},
	actions: {
		request(content, data) {
			return new Promise((resolve) => {
				uni.request({
					url: content.state.url + data.url,
					method: data.method || "POST",
					header: {
						// "ucodelosan":token(),
						'Access-Control-Allow-Origin': '*',
						'Accept': 'application/json',
						'Content-Type': data.method == 'GET' ?
							'application/json' : 'application/x-www-form-urlencoded',
					},
					data: data.data,
					success: (res) => {
						console.log(res.data);
						if (res.data.code == 401) {
							uni.showToast({
								title: res.data.msg || '',
								icon: "none"
							})
							setTimeout(() => {
								uni.redirectTo({
									url: '/pages/login/login'
								})
							}, 1000)
						} else {
							resolve(res.data)
						}
					}
				})
			})
		},
		upload(content, data) {
			return new Promise((resolve) => {
				uni.uploadFile({
					url: content.state.url + content.state.api.uploadImages,
					name: "image",
					filePath: data,
					formData: {
						uid: getId()
					},
					success(res) {
						let obj = JSON.parse(res.data)
						if (obj.code == 1) {
							resolve(obj.avatar)
						} else {
							content.commit('tip', '系统错误')
						}
					},
					fail(res) {
						console.log(res);
					}
				})
			})
		}
	}
})